import time
import os
import csv


from splider.common import CSVUtil
from splider.common import HttpRequest
from splider.common import Progress_Tracker

urlroot = "https://car.autohome.com.cn"


# 品牌列表
def get_series():
    current_dir = os.path.abspath('.')
    print(current_dir)

    brand_csv_path = "data/brand.csv"
    series_csv_path = "data/series.csv"
    point_key = "brand_series"

    series_csv_file = os.path.join(current_dir, series_csv_path)
    brand_csv_file = os.path.join(current_dir, brand_csv_path)
    progress_file = os.path.join(current_dir, "data/progress.txt")

    # 读取当前进度
    Progress_Tracker.load_progress(progress_file)
    # 删除车系中在最后品牌位点处已经写入的行(避免重复写入)
    Progress_Tracker.delete_matching_rows(series_csv_path, brand_csv_path, point_key, 0)

    start_line = Progress_Tracker.get_progress_data()[point_key]



    # 读取并获得所有URL地址，同时记录品牌名称
    with open(brand_csv_file, 'r') as bcf, open(series_csv_file, 'a', newline='') as scf:  # 使用追加模式
        writer = csv.writer(scf)
        reader = csv.reader(bcf)
        for i, row in enumerate(reader):
            if i < start_line:
                continue

            brand_name = row[0]    # 读取第1列
            brand_url = row[2]    #读取第2列

            response = HttpRequest.fetch_url(brand_url)
            if response:
                print("URL=", brand_url)
                # 处理response
            else:
                print("Failed to fetch the page.")

            brands = response.html.find('.cartree ul li dd a')
            for brand in brands:
                bk = brand.text
                #查找 bk 中最后一个左括号 "(" 的位置,赋值给 start
                start = bk.rfind("(")
                #查找 bk 中最后一个右括号 ")" 的位置,赋值给 end
                end = bk.rfind(")")
                #取出左括号前（start 位置之前的子字符串),作为系列名称 series
                series = bk[0:start]
                #取出括号中(左括号之后到右括号之前的子字符串), 作为编号num
                num = bk[(start + 1):end]

                series_url = urlroot + brand.attrs.get("href", None )
                print(brand_name+" "+series + " " +num,series_url)

                CSVUtil.save2csv(writer,brand_name, series.rstrip(), num,series_url)

            scf.flush()
            print(brands)
            # 在循环的最后，保存当前进度
            Progress_Tracker.save_progress(progress_file, point_key, i + 1)
            time.sleep(1)


if __name__ == '__main__':
    get_series()
    print("处理结束")


